<!--#include file="./header.inc"-->
<%

select case aq
	'news
	case "save" 			: 	call save
	case "edit" 			: 	call edit
	case "delete" 			: 	call delete
	case "status" 			: 	call status
	case "editbatch" 		: 	call editBatch
	case "savebatch" 		: 	call saveBatch
	case else 				: 	call index
end select

call db.close()

function index()
	dim keyword
	genreID = admin.joinID(request("genreID"))
	classID = admin.joinID(request("classID"))
	keyword = admin.filter(request("keyword"))
    sql = "select id,picUrl,title,dateTime,hits,auditing,isTop,classID from cj_news where 1=1 "
    if keyword <> "" then sql = sql & " and(title like '%" & keyword & "%' or content like '%" & keyword & "%')"
    if genreID <> 0 then sql = sql & " and genreID in(" & genreID & ")"
    if classID <> 0 then sql = sql & " and classID in(" & classID & ")"
    sql = sql & " Order by id desc"
    temp = db.arrayData(sql, page, rows)
    infoTotal = temp(0)
    data = temp(1)
    %><!--#include file="./view/news.index.html"--><%
end function

function edit()
	genreID = app.int(request("genreID"))
	data = array("", siteName, siteName, "", now(), "", "0", "0", "0", 0, 1, "")
	if iid > 0 then
		sql = "select title,author,copyFrom,hits,dateTime,picUrl,infoViewID,genreID,classID,isTop,auditing,content from cj_news where id=" & iid
		if genreID > 0 then
		    sql = sql & " and genreID=" & genreID
		end if
		set rs = db.exec(sql, 1)
		if rs.eof then admin.message "id不存在！"
		data = rs
		set rs = nothing
	end if
	%><!--#include file="./view/news.edit.html"--><%
end function

function save()
	genreID = app.int(request("genreID"))
	if iid = 0 and genreID = 0 then admin.message "大类ID不能为空!"
    title = admin.filter(request.form("title"))
    author = admin.filter(request.form("author"))
    copyFrom = admin.filter(request.form("copyFrom"))
    content = trim(request.form("content"))
    if author = "" then author = siteName
    if copyFrom = "" then copyFrom = siteTitle
    if len(content) < 10 then admin.message "文章内容字数太少了！"
    content = admin.enUploadDir(content)
    if iid > 0 then
        set rs = db.exec("select * from cj_news where genreID=" & genreID & " and id=" & iid, 3)
        if rs.eof then admin.message "id不存在！"
    else
        set rs = db.exec("select * from cj_news", 3)
        rs.AddNew
        rs("genreID") = genreID
    end if
    rs("title") = title
    rs("author") = author
    rs("content") = content
    rs("copyFrom") = copyFrom
    rs("classID") = app.int(request.form("classID"))
    rs("dateTime") = app.cj_date(request.form("dateTime"))
    rs("picUrl") = admin.filter(request.form("picUrl"))
    rs("hits") = app.int(request.form("hits"))
    rs("isTop") = admin.isCheck(request.form("isTop"))
    rs("auditing") = admin.isCheck(request.form("auditing"))
    rs("infoViewID") = app.int(request.form("infoViewID"))
    rs.update
    rs.close : set rs = nothing
    if iid > 0 then
        admin.back "修改成功！", "返回列表", "?q=index"
    else
        admin.back "添加成功！", "继续添加", "?q=edit"
    end if
end function

function editBatch()
	%><!--#include file="./view/news.editBatch.html"--><%
end function

function saveBatch()
	dim genreID, cid, isClassID, isGenreID, isHits, isDateTime, isInfoViewID, isAuthor, isCopyFrom, iInfoViewID
	genreID = app.int(request.form("genreID"))
	
	isHits = admin.isCheck(request.form("isHits"))
	isGenreID = admin.isCheck(request.form("isGenreID"))
	isClassID = admin.isCheck(request.form("isClassID"))
	isInfoViewID = admin.isCheck(request.form("isInfoViewID"))
	isDateTime = admin.isCheck(request.form("isDateTime"))
	isAuthor = admin.isCheck(request.form("isAuthor"))
	isCopyFrom = admin.isCheck(request.form("isCopyFrom"))
	if not(isHits) and not(isGenreID) and not(isClassID) and not(isDateTime) and not(isInfoViewID) and not(isAuthor) and not(isCopyFrom) then admin.message "需要选择一项！"
	if isGenreID then
		if genreID < 3 then admin.message "选择的ID不属于文章大类！"
		if not(isArray(app.getGenreInfo(genreID))) then admin.message "文章大类不存在！"
	end if
	if isClassID then
		cid = app.int(request.form("classID"))
		if isArray(app.getClassInfo(cid)) then
			if app.getClassInfo(cid)(1) < 3 then admin.message "不属于文章类栏目分类！"
		else
			admin.message "栏目分类不存在！"
		end if
	end if
	if isInfoViewID then
		iInfoViewID = app.int(request.form("infoViewID"))
	end if
	
	dim dateTime, hits, author, copyFrom
	dateTime = app.cj_date(request.form("dateTime"))
	hits = app.int(request.form("hits"))
	author = admin.filter(request.form("author"))
	copyFrom = admin.filter(request.form("copyFrom"))
	set rs = db.exec("select * from cj_news where id in(" & batchId & ")", 3)
	do until rs.eof
		if isGenreID then rs("genreID") = genreID
		if isClassID then rs("classID") = cid
		if isInfoViewID then rs("infoViewID") = iInfoViewID
		if isDateTime then rs("dateTime") = dateTime
		if isHits then rs("hits") = hits
		if isAuthor then rs("author") = author
		if isCopyFrom then rs("copyFrom") = copyFrom
		rs.update
		rs.movenext
	loop
	rs.close : set rs = nothing
	admin.back "修改成功！", "返回列表首页", "?q=index"
end function

function delete()
	call db.exec("Delete from cj_news where id in(" & batchId & ")", 1)
	app.json "删除成功！", 0
end function

function status()
	dim auditing, isTop, msg
	auditing = request("auditing")
	if auditing <> "" then
        auditing = app.cj_bool(auditing)
        sql = " auditing=" & auditing
        msg = "取消审核"
        if auditing then
            msg = "审核"
        end if
    else
        isTop = app.cj_bool(request("isTop"))
        sql = " isTop=" & isTop
        msg = "取消置顶"
        if auditing then
            msg = "置顶"
        end if
    end if
	call db.exec("update cj_news set " & sql & " where id in(" & batchId & ")", 1)
	app.json msg & "成功！", 0
end function

%>